package com.niannian.activity;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.niannian.BaseActivity;
import com.niannian.R;
import com.niannian.dialog.TipDialog;
import com.niannian.dialog.TipDialog.onSimpleOnCancelListener;
import com.niannian.dialog.TipDialog.onSimpleOnClicklListener;
import com.niannian.util.AsyncHandle;
import com.niannian.util.Common;
import com.niannian.util.ErrorCode;
import com.niannian.util.MyTextWatcher;
import com.niannian.util.UmengPage;
import com.niannian.web.nnSyncApi;
import com.umeng.analytics.MobclickAgent;

/**
 * 
 * 注册第二步
 * 
 * @author shengl
 * 
 */
public class RegisterTwoActivity extends BaseActivity {
	private EditText reg_captcha_register;
	private Button btn_register_two, btn_get_new_captcha;
	String captcha = "";
	String acct = "";
	String passwd = "";
	Handler timehandler;
	Timer timer;
	int crrtime = 60;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_register_two);

		initView();
		setLister();
		// initDialog();
		initTopView();
		Bundle b = getIntent().getExtras();
		if (b != null) {
			acct = b.getString("phone");
			passwd = b.getString("passwd");
		}
		timehandler = new Handler() {

			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:
					crrtime--;
					if (crrtime == 0) {
						btn_get_new_captcha.setText("重新获取");
						btn_get_new_captcha.setEnabled(true);
						timer.cancel();
					} else {
						btn_get_new_captcha.setText("(" + crrtime + ")重新获取");
						btn_get_new_captcha.setEnabled(false);
					}

					break;
				}
				super.handleMessage(msg);
			}
		};
		time();
		addActToGroup("Register", this);
	}

	private void initView() {
		reg_captcha_register = (EditText) findViewById(R.id.reg_captcha_register);
		btn_register_two = (Button) findViewById(R.id.btn_register_two);
		btn_get_new_captcha = (Button) findViewById(R.id.btn_get_new_captcha);
		reg_captcha_register.addTextChangedListener(textWatcher);

	}

	private void setLister() {

		btn_register_two.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				captcha = reg_captcha_register.getText().toString().trim();
				if (captcha.length() != 6) {
					Common.tip(RegisterTwoActivity.this, "请输入正确的6位数验证码");
					return;
				}

				new signup1().init(RegisterTwoActivity.this, null, true, "请稍后...").execute();
			}
		});

		btn_get_new_captcha.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				new GetNewCaptcha().init(RegisterTwoActivity.this, null, true, "获取验证码中...").execute();
			}
		});

	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("注册");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);

		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				openActivity(LoginBeforeActivity.class);
				myfinish();
				// tip.show();
			}
		});
	}

	TipDialog tip;

	private void initDialog() {
		tip = new TipDialog(RegisterTwoActivity.this, "确认要放弃注册吗?");
		tip.setOnCancelListener(new onSimpleOnCancelListener() {

			@Override
			public void onCancel() {
				tip.dismiss();
			}
		});
		tip.setOnConfimClickListener(new onSimpleOnClicklListener() {

			@Override
			public void onClick() {
				openActivity(LoginBeforeActivity.class);
				myfinish();
				tip.dismiss();
			}
		});
	}

	private MyTextWatcher textWatcher = new MyTextWatcher() {

		@Override
		public void onFocusChange(View view, boolean hasFocus) {
		};

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			if (!Common.empty(reg_captcha_register.getText().toString().trim())) {
				//btn_register_two.setEnabled(true);
			} else {
				//btn_register_two.setEnabled(false);
			}
		}
	};

	protected class signup1 extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {
			Common.tip(RegisterTwoActivity.this, "验证验证码失败");
		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {
			int retCode = paramJSONObject.getInt("code");
			if (retCode == ErrorCode.OK) {
				Common.tip(RegisterTwoActivity.this, "验证验证码成功");

				try {
					// userInfoManager.Json2DBUser(paramJSONObject
					// .getJSONObject("data"));
					MobclickAgent.onEvent(RegisterTwoActivity.this, UmengPage.SIGNUP_PRESS_OK);
					userInfoManager.id = paramJSONObject.getInt("uid");
					userInfoManager.family = paramJSONObject.getInt("fid");
					update_gUser();
					openActivity(RegisterThereActivity.class);
					myfinish();
					startAnimationLeftToRight();
					destroyGroup("Register");
				} catch (Exception e) {
					destroyGroup("Register");
					myfinish();
					Common.tip(RegisterTwoActivity.this, "更新用户资料失败");
				}

			} else {
				Common.tip(RegisterTwoActivity.this, paramJSONObject.getString("msg"));
			}
		}

		protected String runTask(Map<String, Object> paramMap) {
			return nnSyncApi.signup1(acct, passwd, captcha);
		}
	}

	protected class GetNewCaptcha extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {
			Common.tip(RegisterTwoActivity.this, "获取验证码失败");
		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {
			int retCode = paramJSONObject.getInt("code");
			if (retCode == ErrorCode.OK) {
				Common.tip(RegisterTwoActivity.this, "获取验证码成功");
			} else {
				Common.tip(RegisterTwoActivity.this, paramJSONObject.getString("msg"));
			}
		}

		protected String runTask(Map<String, Object> paramMap) {
			return nnSyncApi.verifyPhone(acct, "signup");
		}
	}

	/**
	 * 计时器
	 */
	public void time() {
		TimerTask task = new TimerTask() {
			public void run() {
				Message message = new Message();
				message.what = 1;
				timehandler.sendMessage(message);
			}
		};

		timer = new Timer(true);
		timer.schedule(task, 1000, 1000); // 延时1000ms后执行，1000ms执行一次
		// timer.cancel(); //退出计时器
	}

	@Override
	public void onBackPressed() {
		// tip.show();
		openActivity(LoginBeforeActivity.class);
		myfinish();
	}
}
